import { omit, find } from 'lodash'

export default function (state = { visible: true, widthArr: [] }, action) {
  let res = omit(action, ['type'])

  switch (action.type) {
    case 'setVisible':
      return { ...state, ...res }
    case 'setAsideWidth':
      return { ...omit(state, ['widthArr']), widthArr: setAsideWidth(state, action) }
    default:
      return state
  }
}

function setAsideWidth({ widthArr }, { pos, width }) {
  const obj = find(widthArr, ['pos', pos])
  obj ? (obj.width = width) : widthArr.push({ pos, width })
  return widthArr
}
